﻿/******************************************************************************
 *  LOG_ENABLED
 * 
 *  Enables / disables logging features throughout the appplication.
 *****************************************************************************/
#define LOG_ENABLED

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Threading;

using AFCEPF.BankApplication.BLL;
using AFCEPF.BankApplication.DAL;

namespace AFCEPF.BankApplication.UI.Console
{
    class Program
    {

        private static Logger _logger;

        static Program()
        {
            _logger = Logger.GetLogger(new Program(), Logger.LoggerOptions.Standard );
        }

        static void Main(string[] args)
        {

            

            //TestMappedProperties();
            //test();          
            // Test_GeneratedSQL();
            //Test_CRUD();
            //TestReflection();
            //PrintCountryList();
            

            Thread thread = new Thread(OnDoWork);
            Thread thread2 = new Thread(OnDoWork);
            //thread.Start();
            thread2.Start();
            
            //OnDoWork();



            Log("Finished. Press a key please.");
            System.Console.Read();


        }

        static void OnDoWork()
        {
            var b = new BankingSystemPlayer(1000);

            var query = (from name in b._names.Randomize()
                        where name.Contains("a")
                        select name).Distinct();


            foreach (var name in query.Take(20))
            {
                System.Console.WriteLine(name);
            }

            foreach (var country in b.Countries.Values.ToList().Shuffle())
            {
                System.Console.WriteLine(country);
            }

            Log("Finished Player", query.Distinct().Count().ToString());
        }










        public static void Log(string text)
        {
            Log("{0}", text);
        }

        public static void Log(string format, params string[] args)
        {
            if (_logger == null)
            {
                _logger = Logger.GetLogger(new Program());
            }
            _logger.Log(format, args);
        }


        

        public static void TestReflection()
        {
            var query = from n in typeof(Person).Assembly.GetTypes()
                        where n.GetMembers().Count() > 5
                        select n.FullName;

            foreach (var typeName in query)
            {
                System.Console.WriteLine(   typeName);
            }
        }






        private static void Test_GeneratedSQL()
        {
            // Syntaxe avec un "initialisateur d'objet"..
            // (peut remplacer un constructeur absent 
            // par exemple).
            var p = new Person()
            {
                Name = "toto",
                FirstName = "titi",
                YearOfBirth = DateTime.Now.Year - 10
            };

            var generator = p.DbAccessor.SqlGenerator;

            System.Console.WriteLine(generator.GenerateSqlFor_Select());
            System.Console.WriteLine(generator.GenerateSqlFor_Delete());
            System.Console.WriteLine(generator.GenerateSqlFor_Update());
            System.Console.WriteLine(generator.GenerateSqlFor_Insert());            
        }
        
        private static void TestMappedProperties()
        {
            Person person = new Person();

            person.Name = "toto";
            person.FirstName = "titi";
            person.YearOfBirth = 1950;

            // on teste le bon fonctionnement de 
            // nos objets MappedProperty..
            foreach (MappedProperty p in person.MappedProperties.Values)
            {
                System.Console.WriteLine(p.PropertyName);
                System.Console.WriteLine(p.PropertyFieldName);
                System.Console.WriteLine(p.IsPrimaryKey);
                System.Console.WriteLine(p.PropertyValue);
                System.Console.WriteLine();

            }
        }

        private static void test()
        {
            Assembly assembly;
            assembly = typeof(Person).Assembly;
            foreach (Type t in assembly.GetTypes())
            {
                System.Console.WriteLine(t.Namespace + "." + t.Name);
            }

            // ou.. 
            //var query = from type in typeof(Person).Assembly.GetTypes()
            //            select type;
            //foreach (var item in query)
            //{
            //    System.Console.WriteLine(item.Namespace + "." + item.Name);
            //}

        }

        private static void Test_CRUD()
        {
            // Select
            Person p = new Person(1);

            //System.Console.WriteLine(p.Name);
            
            //// Update
            p.Name = "ssssssssss";
            p.Save();


            // Delete
            //var p1 = new Person(6);
            //System.Console.WriteLine(p1.DbAccessor.SqlGenerator.GenerateSqlFor_Delete());
            //p1.Delete();

            p = new Person()
            {
                Name = "zz",
                FirstName = "xx",
                YearOfBirth = DateTime.Now.Year - 41
            };

            p.Save();
            System.Console.WriteLine(p.Id);
        }

    }
}
